//
//  LoginView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/21.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

protocol loginViewDelegate {
    //改变导航栏标题
    func logSuccess()

    //关闭当前视图
    func closeSelf()
}

class LoginView: TableBaseViewController,LoginUpViewDelegate,LoginDownViewDelegate,QQToolDelegate,WXToolDelegate,TableDelegate {

    
    var delegate : loginViewDelegate?
    
    //QQ工具类
    let qqT = QQTool()
    
    //微信工具类
    let wxT = WXTool()
    
    //左边的导航栏按钮
    lazy var leftBarItemBtn : UIButton = {
        
        let leftBarItemBtn = BlockBtn(type: UIButtonType.custom)
        
        leftBarItemBtn.backgroundImage(for: .normal)
        
        //正常和高亮状态设置为同一张照片
        leftBarItemBtn.setImage(UIImage.init(named: "close"), for: .normal)
        leftBarItemBtn.setImage(UIImage.init(named: "close"), for: .highlighted)
        leftBarItemBtn.frame = CGRect(x: 15, y: 30, width: 20, height: 20)
        leftBarItemBtn.setTitle("BlockButton", for: UIControlState())
        leftBarItemBtn.addTarget(self, action: #selector(LoginView.dismiss as (LoginView) -> () -> Void), for: .touchUpInside)
        
        
        return leftBarItemBtn
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        self.navigationController?.navigationBar.isHidden = false
        
        self.navigationItem.title = "登陆"
        
        //继承基础类的代理
        self.t_delegate = self
        
        //取消返回手势,防止用户不小心返回页面，输入的个人信息丢失
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    
    //页面结束后，恢复左滑手势
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }

    
    deinit {
    
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "123"), object: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //标题
        let centerTitle = UILabel()
        centerTitle.frame = CGRect(x: 0, y: 25, width: UIScreen.main.bounds.width, height: 30)
//        centerTitle.backgroundColor = UIColor.red
        centerTitle.font = UIFont.boldSystemFont(ofSize: 16)
        
        centerTitle.text = "登陆"
        centerTitle.textAlignment = NSTextAlignment.center
        
        setUpView()
        setDownView()

        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }

    //关闭当前视图
    func dismiss() -> Void {
        delegate?.closeSelf()
        
        self.dismiss(animated: true, completion: nil)
        
    }

}


extension LoginView {
    func setUpView() -> Void {
        
        let upView = LoginUpView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.6 ))
        
        //监听代理
        upView.delegate = self
        
        view.addSubview(upView)
    }
    
    func setDownView() -> Void {
        //
        let downView = LoginDownView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height * 0.6, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.4 - 64))
        //监听代理
        downView.delegate = self
        
        view.addSubview(downView)
    }
}

// MARK:- 监听代理
extension LoginView {
    //注册事件
    func rigSEL() {
        self.hidesBottomBarWhenPushed = true
         self.navigationController?.pushViewController(RigisterViewController(), animated: true)
    }
    
    //忘记密码事件
    func forSEL() {

        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(ForgerPassViewController(), animated: true)
    }
    
    ///QQ代理事件
    func qqLoginSEL() {
        qqT.qqLogin()
        qqT.delegate = self
        
    }
    ///微信登陆
    func wxloginSEL() {
//        wxT.clickAuto()
        
        wxT.clickAuto { (result) in
            if result == "授权成功" {
                print(result)
                CustomAlertView.shared.alertWithTitle(strTitle: authSuccess)
            }
        }
        wxT.delegate = self
        
    }
    
    ///登陆事件
    func logSE() {
        
    }
    
    func logSuc() {

        self.dismiss()

    }
    
}


extension LoginView {
    func qqLoginCallBack() {
        
        print("qqloginsuccess")
        
        self.dismiss()
        self.navigationController!.popToRootViewController(animated: true)

    }
    
    //微信登陆成功回调
    func WXLoginCallBack() {
        self.dismiss()
        
        self.navigationController!.popToRootViewController(animated: true)

        print("hhhh")
        print("微信登陆成功回调")
    }

}

// MARK:- 实现基类的方法
extension LoginView {
    func click() {
        
        let alert = UIAlertController.init(title: "提示", message: "您确定要放弃当前编辑的内容么?", preferredStyle: .alert)
        
        alert.addAction(UIAlertAction.init(title: "放弃编辑", style: .destructive, handler: { (nil) in
            //            self.navigationController!.popViewController(animated: true)
            // pop here
            if let navController = self.navigationController {
                navController.popViewController(animated: true)
            }
        }))
        
        alert.addAction(UIAlertAction.init(title: "留在此页", style: .default, handler: nil))
        
        self.navigationController?.present(alert, animated: true, completion: nil)
        
        
    }
    
}
